package com.yuxi.crystal.animation;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

/**
 * 属性动画
 */
public class PropertyAnimation extends AppCompatActivity {
    ImageView ivIconDelete =null;//用于显示动画的图像
    RadioGroup rgControl = null;//用于控制图像的变化
    private String TAG=getClass().getSimpleName()+" ";
    private  int startx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_animation);
        initView();//初始化视图
    }

    /**
     * 初始化视图
     */
    private void initView() {
        ivIconDelete= (ImageView) findViewById(R.id.iv_ic_delete);
        rgControl= (RadioGroup) findViewById(R.id.rg_control);
        rgControl.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                startAnimation(checkedId);//根据不同的点击id来做不同的反馈
            }
        });
        startx=ivIconDelete.getLeft();
    }

    private synchronized void startAnimation(int checkedId) {
        ValueAnimator animation = ValueAnimator.ofInt(ivIconDelete.getLeft(),ivIconDelete.getLeft()+300);
        animation.setDuration(3000);
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                String update= animation.getAnimatedValue().toString();
                Log.d(TAG, "onAnimationUpdate: update = "+update);
                LinearLayout.LayoutParams paran = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                paran.leftMargin= (int) animation.getAnimatedValue();
                ivIconDelete.setLayoutParams(paran);
            }
        });
       animation.addListener(new Animator.AnimatorListener() {
           @Override
           public void onAnimationStart(Animator animation) {

           }

           @Override
           public void onAnimationEnd(Animator animation) {
               Log.i(TAG, "onAnimationEnd: ");
               LinearLayout.LayoutParams paran = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
               paran.leftMargin= startx;
               ivIconDelete.setLayoutParams(paran);
           }

           @Override
           public void onAnimationCancel(Animator animation) {

           }

           @Override
           public void onAnimationRepeat(Animator animation) {

           }
       });
        switch (checkedId){
            case R.id.rb_accelerate_interpolator://加速
                Log.i(TAG, "startAnimation: 加速");
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                break;
            case R.id.rb_decelerate_interpolator://减速
                animation.setInterpolator(new DecelerateInterpolator());
                break;
            case R.id.rb_accelerate_decelerate_interpolator://先加速，后减速
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                break;
            case R.id.rb_anticipate_interpolator://反向 ，先向相反方向改变一段再加速播放
                animation.setInterpolator(new AnticipateInterpolator());
                break;
            case R.id.rb_anticipate_overshoot_interpolator://反向加超越，先向相反方向改变，再加速播放，会超出目的值然后缓慢移动至目的值
                animation.setInterpolator(new AnticipateOvershootInterpolator());
                break;
            case R.id.rb_bounce_interpolator://-跳跃，快到目的值时值会跳跃
                animation.setInterpolator(new BounceInterpolator());
                break;
            case R.id.rb_cycle_interpolator://循环，动画循环一定次数，值的改变为一正弦函数：Math.sin(2 * mCycles * Math.PI * input)
                animation.setInterpolator(new CycleInterpolator(3));
                break;
            case R.id.rb_linear_interpolator://线性
                animation.setInterpolator(new LinearInterpolator());
                break;
            case R.id.rb_overshot_interpolator://超越，最后超出目的值然后缓慢改变到目的值
                animation.setInterpolator(new AnticipateOvershootInterpolator());
                break;

        }
        animation.start();
    }

    /**
     * 加速渐变
     */
    public void AlphaAccelerate(){
         ObjectAnimator animatoer = ObjectAnimator.ofFloat(ivIconDelete,"alpha",0f,1f);
                animatoer.setDuration(3000);
                animatoer.start();
    }
}
